package com.itheima.admin.controller;

import com.itheima.admin.service.IRoleService;
import com.itheima.pojo.Config.Result;
import com.itheima.pojo.dto.PageDto;
import com.itheima.pojo.dto.RoleDto;
import com.itheima.pojo.vo.PageVo;
import com.itheima.pojo.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
@Api(value = "角色模块接口",tags = "admin_role",description = "用于实现后台角色的增删改查操作")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @PostMapping("/list")
    @ApiOperation("分页查询用户列表数据")
    public PageVo<RoleVo> queryByPage(@RequestBody PageDto pageDto){
        if (pageDto.getCurrentPage() == null) {
            pageDto.setCurrentPage(1);
        }
        if (pageDto.getPageSize() == null) {
            pageDto.setPageSize(10);
        }
        return roleService.queryByPage(pageDto);
    }

    @GetMapping("/all")
    public List<RoleVo> queryAll() {
        return roleService.queryAll();
    }

    @PostMapping("/add")
    public Result addRole(@RequestBody RoleDto roleDto){
        Boolean flag=roleService.addRole(roleDto);
        Result result = Result.builder()
                .flag(flag)
                .message(flag?"新增角色成功!":"新增角色失败")
                .build();
        return result;
    }

    @DeleteMapping("/{id}")
    public Result deleteRole(@PathVariable("id") String id){
        //角色和用户
        //角色和模块
        //角色
        Boolean flag=roleService.deleteRole(id);
        Result result = Result.builder()
                .flag(flag)
                .message(flag?"删除角色成功!":"删除角色失败")
                .build();
        return result;
    }

    @PostMapping("/auth")
    public Result roleAuth(Map map){
        Boolean flag=roleService.roleAuth(map);
        Result result = Result.builder()
                .flag(flag)
                .message(flag?"授权成功!":"授权失败")
                .build();
        return result;
    }
}
